package com.lintf.bot.rabbitmq;

import com.lintf.bot.rabbitmq.constant.Constants;
import com.lintf.bot.rabbitmq.dead.CommonMessageRetryService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @className: DeadLetterConsumer
 * @Description: TODO 死信队列消费者
 * @author: wzq
 * @date: 2022/5/13 3:05 PM
 */
@Slf4j
@Component
public class DeadLetterConsumer extends CommonMessageRetryService {

    @RabbitHandler
    @RabbitListener(queues = Constants.DEAD_LETTER_EXCHANGE_QUEUE, ackMode = "MANUAL")
    public void consumer(Message message, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) throws IOException {
        try {
            log.info("【死信队列】【" + System.currentTimeMillis() + "】收到死信队列消息：", new String(message.getBody()));
            retryMessage(message);
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            channel.basicNack(deliveryTag, false, false);
        }

    }
}

